/*
 * @Author: wangming
 * @Date: 2022-06-23 17:01:36
 * @LastEditors: wangming
 * @LastEditTime: 2022-07-12 14:39:30
 * @FilePath: /hanma-paas-mobile-fed/Users/wm/瀚川/hanma-application-designer-fed/src/components/BlocklyEditor/microApp.js
 * @Description: 
 */
import microApp, { EventCenterForMicroApp } from "@micro-zoe/micro-app";

const blocklyUrl = {
  debug: "https://hanma-paas-dev.hancode.com/blockly/",
  // 远程连远程
  remote: `${location.protocol}//${location.hostname}/blockly/`,
};

window.urlForBlockMakerApp =
  process.env.NODE_ENV === "production" ? blocklyUrl.remote : blocklyUrl.debug;

microApp.start({
  plugins: {

    modules: {
      blockly: [
        {
          loader(code) {
            if (window.urlForBlockMakerApp === blocklyUrl.local) {
              code = code.replace(
                /(from|import)(\s*['"])(\/blockly\/)/g,
                (all) => {
                  return all.replace(
                    "/blockly/",
                    `${window.urlForBlockMakerApp}`
                  );
                }
              );
            } else {
              code = code
                .replace(
                  /(from|import)\s*['"](\.\/(.+\.js))['"]/g,
                  (all, p1, p2, p3) => {
                    const url = `${window.urlForBlockMakerApp}/assets/${p3}`;
                    return all.replace(p2, url);
                  }
                )
            }
            return code;
          },
        },
      ],
    },
  },
});

// https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/framework/vite?id=%f0%9f%91%87-%e6%95%b0%e6%8d%ae%e9%80%9a%e4%bf%a1
window.eventCenterForBlocklyApp = new EventCenterForMicroApp("blockly");
